import api from '../../../api'
import types from '../../types'

export function getHomeList(pageNo, pageSize) {
    return function (dispatch) {
        return api.post('/proxy/wemall/goods/list', {
            params: {},
            data: {
                "pageNo": pageNo,
                "pageSize": pageSize
            }
        }).then(response => {
            dispatch(getHomeListSuccess(response));
        })
    }
}

export function getHomeListSuccess(data) {
    return {
        type: `${types.GET_HOME_LIST}_SUCCESS`,
        payload: {
            homeList: data
        }
    }
}

export function getMenuList(pageNo, pageSize) {
    return function (dispatch) {
        return api.post('/proxy/wemall/menu/list', {
            params: {},
            data: {
                "pageNo": pageNo,
                "pageSize": pageSize
            }
        }).then(response => {
            dispatch(getMenuListSuccess(response));
        })
    }
}

export function getMenuListSuccess(data) {
    return {
        type: `${types.GET_MENU_LIST}_SUCCESS`,
        payload: {
            menuInfo: data
        }
    }
}

export function getGoodList(menuId, pageNo, pageSize) {
    return function (dispatch) {
        return api.post('/proxy/wemall/goods/formenu', {
            params: {},
            data: {
                "menuId": menuId,
                "pageNo": pageNo,
                "pageSize": pageSize
            }
        }).then(response => {
            dispatch(getGoodListSuccess(response));
        })
    }
}
export function getGoodListSuccess(data) {
    return {
        type: `${types.GET_GOOD_LIST}_SUCCESS`,
        payload: {
            goodInfo: data
        }
    }
}

export function getGoodInfo(goodId) {
    return function (dispatch) {
        return api.post('/proxy/wemall/goods/info', {
            params: {},
            data: {
                "id": goodId
            }
        }).then(response => {
            dispatch(getGoodInfoSuccess(response));
        })
    }
}
export function getGoodInfoSuccess(data) {
    return {
        type: `${types.GET_GOOD_INFO_LIST}_SUCCESS`,
        payload: {
            goodInfo: data
        }
    }
}

export function addOrder(userName, address, phone, payStyle, note, toralPrice, cartData) {
    return function (dispatch) {
        return api.post('/proxy/wemall/order/add', {
            params: {},
            data: {
                "userName": userName,
                "address": address,
                "phone": phone,
                "payStyle": payStyle,
                "note": note,
                "totalPrice": toralPrice,
                "cartData": cartData
            }
        }).then(response => {
            dispatch(addOrderSuccess(response));
        })
    }
}
export function addOrderSuccess(data) {
    return {
        type: `${types.ADD_ORDER}_SUCCESS`,
        payload: {
            msg: data
        }
    }
}

export function getOrder() {
    return function (dispatch) {
        return api.post('/proxy/wemall/order/list', {
            params: {},
            data: {
                "pageNo": 1,
                "pageSize": 10
            }
        }).then(response => {
            dispatch(getOrderSuccess(response));
        })
    }
}
export function getOrderSuccess(data) {
    return {
        type: `${types.GET_ORDER}_SUCCESS`,
        payload: {
            orderInfo: data
        }
    }
}